package com.zhang.passer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.passer.domain.*;
import com.zhang.passer.mapper.*;
import com.zhang.passer.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private JingMapper jingMapper;

    @Autowired
    private MeishiMapper msMapper;

    @Autowired
    private YouJiMapper yjMapper;

    @Autowired
    private JiuDianMapper houseMapper;

    @Autowired
    private ImageMapper imageMapper;


    public Map<String, Object> getSearchResult(String name) {
        List<JingDo> jings = jingMapper.selectList(new QueryWrapper<JingDo>().like(true,"jname",name));
        for (JingDo jing : jings) {//填充列表图片
            ArrayList<ImageDo> images = new ArrayList<>();
            images.add(imageMapper.selectOne(new QueryWrapper<>(new ImageDo().setJid(jing.getJid())).last("order by sid desc limit 1")));
            jing.setImages(images);
        }
        List<MeiShiDo> meiShis = msMapper.selectList(new QueryWrapper<MeiShiDo>().like(true,"mname",name));
        for (MeiShiDo meiShi : meiShis) {//填充列表图片
            ArrayList<ImageDo> images = new ArrayList<>();
            images.add(imageMapper.selectOne(new QueryWrapper<>(new ImageDo().setMid(meiShi.getMid())).last("order by sid desc limit 1")));
            meiShi.setImages(images);
        }
        List<YouJiDo> youJis = yjMapper.selectList(new QueryWrapper<YouJiDo>().like(true,"title",name));
        for (YouJiDo youji : youJis) {//填充列表图片和发表用户信息
            ArrayList<ImageDo> images = new ArrayList<>();
            images.add(imageMapper.selectOne(new QueryWrapper<>(new ImageDo().setYid(youji.getYid())).last("order by sid desc limit 1")));
            youji.setImages(images);
            youji.setUser(new UserSafe().setUid(1).setNickname("小张"));
        }
        List<JiuDianDo> jiuDians = houseMapper.selectList(new QueryWrapper<JiuDianDo>().like(true,"name",name));
        for (JiuDianDo jiudian : jiuDians) {//填充列表图片
            ArrayList<ImageDo> images = new ArrayList<>();
            images.add(imageMapper.selectOne(new QueryWrapper<>(new ImageDo().setHid(jiudian.getHid())).last("order by sid desc limit 1")));
            jiudian.setImages(images);
        }

        Map<String, Object> result = new HashMap<>();

        result.put("jings",jings);
        result.put("meiShis",meiShis);
        result.put("youJis",youJis);
        result.put("jiuDians",jiuDians);

        return result;
    }
}
